package com.bai.ruiji.Common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义元数据注解
 * @author 27471
 * Time : 2022/10/9 17:10
 */
@Component//添加到spring里
@Slf4j
public class MyMetaObjectHandle implements MetaObjectHandler {
    /**
     * 执行inset(插入)语句时执行自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]");
        log.info(metaObject.toString());

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    /**
     * 执行update(更新)语句时进行字段填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
       log.info("公共字段进行填充[update]");
       log.info(metaObject.toString());

        metaObject.setValue("updateTime",LocalDateTime.now());
       metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }
}
